//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Categories;
using NopSolutions.NopCommerce.Common.Manufacturers;
using NopSolutions.NopCommerce.Common.Media;
using NopSolutions.NopCommerce.Common.Products;
using NopSolutions.NopCommerce.Common.Promo.Discounts;
using NopSolutions.NopCommerce.Common.Tax;
using NopSolutions.NopCommerce.Common.Templates;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Common.Warehouses;
using NopSolutions.NopCommerce.Web.Administration.Modules;
 
namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ProductInfoAddControl : BaseNopAdministrationUserControl
    {
        private void FillDropDowns()
        {
            this.ddlTemplate.Items.Clear();
            ProductTemplateCollection productTemplateCollection = TemplateManager.GetAllProductTemplates();
            foreach (ProductTemplate productTemplate in productTemplateCollection)
            {
                ListItem item2 = new ListItem(productTemplate.Name, productTemplate.ProductTemplateID.ToString());
                this.ddlTemplate.Items.Add(item2);
            }

            this.ddlProductType.Items.Clear();
            ProductTypeCollection productTypeCollection = ProductManager.GetAllProductTypes();
            foreach (ProductType productType in productTypeCollection)
            {
                ListItem item2 = new ListItem(productType.Name, productType.ProductTypeID.ToString());
                this.ddlProductType.Items.Add(item2);
            }

            this.ddlTaxCategory.Items.Clear();
            ListItem itemTaxCategory = new ListItem("---", "0");
            this.ddlTaxCategory.Items.Add(itemTaxCategory);
            TaxCategoryCollection taxCategoryCollection = TaxCategoryManager.GetAllTaxCategories();
            foreach (TaxCategory taxCategory in taxCategoryCollection)
            {
                ListItem item2 = new ListItem(taxCategory.Name, taxCategory.TaxCategoryID.ToString());
                this.ddlTaxCategory.Items.Add(item2);
            }

            this.ddlWarehouse.Items.Clear();
            ListItem itemWarehouse = new ListItem("---", "0");
            this.ddlWarehouse.Items.Add(itemWarehouse);
            WarehouseCollection warehouseCollection = WarehouseManager.GetAllWarehouses();
            foreach (Warehouse warehouse in warehouseCollection)
            {
                ListItem item2 = new ListItem(warehouse.Name, warehouse.WarehouseID.ToString());
                this.ddlWarehouse.Items.Add(item2);
            }

            CommonHelper.FillDropDownWithEnum(this.ddlLowStockActivity, typeof(LowStockActivityEnum));
        }

        private void TogglePanels()
        {
            pnlDownloadFile.Visible = cbIsDownload.Checked;
            pnlStockQuantity.Visible = cbManageStock.Checked;
            pnlMinStockQuantity.Visible = cbManageStock.Checked;
            pnlLowStockActivity.Visible = cbManageStock.Checked;
            pnlNotifyForQuantityBelow.Visible = cbManageStock.Checked;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillDropDowns();
                this.TogglePanels();
            }
        }

        protected void cbManageStock_CheckedChanged(object sender, EventArgs e)
        {
            this.TogglePanels();
        }

        protected void cbIsDownload_CheckedChanged(object sender, EventArgs e)
        {
            this.TogglePanels();
        }
        
        public Product SaveInfo()
        {
            DateTime? availableStartDateTime = null;
            DateTime? availableEndDateTime = null;
            DateTime availableStartDateTimeTmp = DateTime.Now;
            if (DateTime.TryParse(txtAvailableStartDateTime.Text, out availableStartDateTimeTmp))
            {
                availableStartDateTime = DateTime.SpecifyKind(availableStartDateTimeTmp, DateTimeKind.Utc);
            }
            DateTime availableEndDateTimeTmp = DateTime.Now;
            if (DateTime.TryParse(txtAvailableEndDateTime.Text, out availableEndDateTimeTmp))
            {
                availableEndDateTime = DateTime.SpecifyKind(availableEndDateTimeTmp, DateTimeKind.Utc);
            }

            Download productVariantDownload = null;
            HttpPostedFile productVariantDownloadFile = fuProductVariantDownload.PostedFile;
            if ((productVariantDownloadFile != null) && (!String.IsNullOrEmpty(productVariantDownloadFile.FileName)))
            {
                byte[] productVariantDownloadBinary = DownloadManager.GetDownloadBits(productVariantDownloadFile.InputStream, productVariantDownloadFile.ContentLength);
                productVariantDownload = DownloadManager.InsertDownload(productVariantDownloadBinary, productVariantDownloadFile.ContentType, Path.GetExtension(productVariantDownloadFile.FileName), true);
            }
            int productVariantDownloadID = 0;
            if (productVariantDownload != null)
                productVariantDownloadID = productVariantDownload.DownloadID;

            DateTime nowDT = DateTime.Now;
            Product product = ProductManager.InsertProduct(txtName.Text, txtShortDescription.Text, txtFullDescription.Value, txtAdminComment.Text,
                 int.Parse(this.ddlProductType.SelectedItem.Value), int.Parse(this.ddlTemplate.SelectedItem.Value),
                 cbShowOnHomePage.Checked, string.Empty,
                 string.Empty, string.Empty, string.Empty,
                 cbAllowCustomerReviews.Checked, cbAllowCustomerRatings.Checked, 0, 0,
                 cbPublished.Checked, false, nowDT, nowDT);


            LowStockActivityEnum lowStockActivity = (LowStockActivityEnum)Enum.ToObject(typeof(LowStockActivityEnum), int.Parse(this.ddlLowStockActivity.SelectedItem.Value));
            ProductVariant productVariant = ProductManager.InsertProductVariant(product.ProductID,
                 string.Empty, txtSKU.Text, string.Empty, string.Empty, txtManufacturerPartNumber.Text,
                 cbIsDownload.Checked, productVariantDownloadID, cbIsShipEnabled.Checked,
                 cbIsFreeShipping.Checked, txtAdditionalShippingCharge.Value,
                 cbIsTaxExempt.Checked, int.Parse(this.ddlTaxCategory.SelectedItem.Value),
                 cbManageStock.Checked, txtStockQuantity.Value,
                 txtMinStockQuantity.Value, lowStockActivity, txtNotifyForQuantityBelow.Value,
                 txtOrderMinimumQuantity.Value, txtOrderMaximumQuantity.Value,
                 int.Parse(this.ddlWarehouse.SelectedItem.Value), cbDisableBuyButton.Checked,
                 cbRequiresTextOption.Checked, txtTextOptionPrompt.Text,
                 txtPrice.Value, txtOldPrice.Value, txtWeight.Value, txtLength.Value,
                 txtWidth.Value, txtHeight.Value, 0,
                 availableStartDateTime, availableEndDateTime,
                 cbPublished.Checked, false, 1, nowDT, nowDT);

            return product;
        }

        public int ProductID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductID");
            }
        }
    }
}